import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import { LoginUsers, users } from './data/user'
import { categoryData, category } from './data/category'
import { telData, tel } from './data/tel_list'


// const Mock = require('mockjs');
//格式： Mock.mock( url, post/get , 返回的数据)；
// let mockCate = Mock.mock('/manage/types', 'get', require('./json/category.js'));
// Mock.mock('/manage/number', 'get', require('./json/info'));

export default {
	datatype(){
		let mock = new MockAdapter(axios)
		// mock success request
		mock.onGet('/success').reply(200, {
			msg: 'success'
		})
		// mock error request
		mock.onGet('/error').reply(500, {
			msg: 'failure'
		})
		// 登录
		mock.onPost('/manage/types').reply(arg => {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					resolve([200, {
						code: 200,
						mockcate: categoryData
					}])
				}, 500)
			})
		})
	},
	datatel(){
		let mock = new MockAdapter(axios)
		// mock success request
		mock.onGet('/success').reply(200, {
			msg: 'success'
		})
		// mock error request
		mock.onGet('/error').reply(500, {
			msg: 'failure'
		})
		// 登录
		mock.onPost('/manage/tel').reply(arg => {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					resolve([200, {
						code: 200,
						mocktel: telData
					}])
				}, 500)
			})
		})
	},
	// 登录
	login() {
		let mock = new MockAdapter(axios)
		// mock success request
		mock.onGet('/success').reply(200, {
			msg: 'success'
		})
		// mock error request
		mock.onGet('/error').reply(500, {
			msg: 'failure'
		})
		// 登录
		mock.onPost('/page/login').reply(arg => {
			
			console.log(arg.data);
			// consloe.log(arg.data); ==>  'username=daizhen&password=daizhen888'
			// let { username, password } = JSON.parse(arg.data)

			let { username, password } = {username: "daizhen", password: "daizhen888"};

			return new Promise((resolve, reject) => {
				let token = null
				let hasUser = LoginUsers.some(u => {
					if (u.username === username && u.password === password) {
						token = u.encrypt_id
						return true
					}
				})
				if (hasUser) {
					resolve([200, { code: 200, msg: '请求成功', token: token }])
				} else {
					resolve([200, { code: 500, msg: '账号或密码错误' }])
				}
			})
		})
	}
}